home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / platforms and tools / debuggerpresence / debugger.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.9 KB  |  163 lines

  1. /*
  2.     File:        debugger.c
  3.  
  4.     Contains:    This is a small snippet of code that can be used to to detect if
  5.                 macsbug is installed or not. NOTE:  This code is intended to only
  6.                 work with version 6.2 of macsbug.  You should refer to your Low Level
  7.                 Debugger's manual for more information on how they install
  8.                 themselves.
  9.     
  10.                 This code is based on information obtained from the MacsBug
  11.                 Reference. The basic assumptions are that macsbug will install itself
  12.                 in the following manner:
  13.         
  14.                 If you are running with a Memory Manager that only works in 24 bit
  15.                 mode, then the high -order byte of MacJmp is a flags byte that
  16.                 contains the following information:
  17.     
  18.                 Bit    Meaning
  19.                 ---    --------------------------------------
  20.                 7    -   Set if debugger is running
  21.                 6    -   Set if debugger can handle system errors
  22.                 5    -   Set if debugger is installed
  23.                 4     -   Set if debugger can support discipline utility
  24.     
  25.                 The lower 3 bytes are used to store the address of the debugger's
  26.                 entry point.
  27.     
  28.                 If you are running with a Memory Manager that works in 32-bit mode,
  29.                 the flags byte is moved to address 0xBFF and the long word at MacJmp
  30.                 becomes a full 32-bit address that points to the debugger's entry
  31.                 point..
  32.     
  33.                 Symantec has a comment in the Think Reference 2.0.1 which states:
  34.     
  35.                 "ADDENDUM:  The above information seems to be incorrect in the
  36.                 reference manual. I have found through testing etc. that in both
  37.                 modes, the Flag Byte appears at location 0xBFF.  The code reflects
  38.                 these findings."
  39.             
  40.                 This is because they confused running in 24 bit mode, running in 32
  41.                 bit mode, and the _ability_ to run in 32 bit mode.  It is the latter
  42.                 ability which you must test to determine the location of the debugger
  43.                 flags.
  44.  
  45.     Written by:     
  46.  
  47.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  48.  
  49.                 You may incorporate this Apple sample source code into your program(s) without
  50.                 restriction. This Apple sample source code has been provided "AS IS" and the
  51.                 responsibility for its operation is yours. You are not permitted to redistribute
  52.                 this Apple sample source code as "Apple sample source code" after having made
  53.                 changes. If you're going to re-distribute the source, we require that you make
  54.                 it clear in the source that the code was descended from Apple sample source
  55.                 code, but that you've made changes.
  56.  
  57.     Change History (most recent first):
  58.                 8/12/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  59.                 
  60.  
  61. */ 
  62. #define TEST    // if you want a standalone procedure, undefine TEST
  63.  
  64. #include <Memory.h>
  65. #include <Gestalt.h>
  66. #include <LowMem.h>
  67.  
  68. /* stuff that should be defined in C, but isn't (it's in Private.a) */
  69. #define    MacJmp            (Ptr *)  0x120        /* MACSBUG jumptable [pointer] */
  70. #define    MacJmpByte        (char *) 0x120        /* MACSBUG flags in 24 bit mode [byte] */
  71. #define MacJmpFlag        (char *) 0xBFF        /* MacsBug flag [byte] */
  72.  
  73. enum dTypes {
  74.     noDebugger,
  75.     macsbug,
  76.     tmon,
  77.     other
  78. };
  79.  
  80. typedef enum dTypes    debuggerTypes;
  81.  
  82. /* stuff for use in this code */
  83. #define    DebuggerInstalled    5
  84. Boolean
  85. GetDebuggerInfo(debuggerTypes *kind, short *signature);
  86. /*
  87. ** GetDebuggerInfo
  88. ** as documented in the "Macsbug Reference & Debugging Guide", page 412
  89. ** if we have a 32 bit capable Memory Manager, debugger flags are at $BFF
  90. ** if we have a 24 bit capable Memory Manager, debugger flags are at $120
  91. ** Ptr at $120 
  92. **
  93. ** Note that the documentation is slightly obscure--you check if the machine
  94. ** is capable of 32-bit mode, _not_ if you are running in 32 bit mode.
  95. **/
  96. Boolean
  97. GetDebuggerInfo(debuggerTypes *kind, short *signature)
  98. {
  99.     short    debugFlags;
  100.     Ptr        debugEntry;
  101.     short    **debugWorld;
  102.     Ptr        ROMBaseWorld;
  103.     long    addressingMode;
  104.     
  105.     /* initialize defaults, assuming no debugger present */
  106.     *kind = noDebugger;
  107.     *signature = '  ';
  108.  
  109.     Gestalt(gestaltAddressingModeAttr, &addressingMode);
  110.     if (addressingMode & (1 << gestalt32BitCapable))
  111.         debugFlags = *MacJmpFlag;
  112.     else
  113.         debugFlags = *MacJmpByte;
  114.  
  115.     if ( debugFlags & (1 << DebuggerInstalled) ) {
  116.  
  117.         /* we've got a debugger. what is it? */
  118.         debugEntry = StripAddress(*MacJmp);
  119.         ROMBaseWorld = StripAddress((Ptr)*(long *)LMGetROMBase());
  120.  
  121.         if (debugEntry < ROMBaseWorld) {    /* not ROM based debugger */
  122.  
  123.             debugWorld = (short **)StripAddress(debugEntry - sizeof(Ptr));
  124.             *signature = **debugWorld;
  125.             switch (*signature) {
  126.                 case 'MT':
  127.                     *kind = macsbug;
  128.                     break;
  129.                 case 'WH':
  130.                     *kind = tmon;
  131.                     break;
  132.                 default:
  133.                     *kind = other;
  134.                     break;
  135.             }
  136.         }
  137.         return true;
  138.     }
  139.     return false;
  140. }
  141.  
  142. #ifdef TEST
  143.  
  144. #include <stdio.h>
  145.  
  146. void main()
  147. {
  148.     debuggerTypes    k;
  149.     union {
  150.         short    sigShort;
  151.         char    sigChar[2];
  152.     } s;
  153.     char    *dbgTypes[] = {"no debugger", "Macsbug", "TMON", "other debugger"};
  154.     printf("Test of debugger info\n");
  155.     
  156.     if (GetDebuggerInfo(&k, &s.sigShort))
  157.         printf("Debugger type is %s and debugger signature is %c%c",
  158.             dbgTypes[k], s.sigChar[0], s.sigChar[1]);
  159.     else
  160.         printf("Didn't find a debugger.\n");
  161. }
  162.  
  163. #endif